import { getBackLog, launchCheck, allCheck, getCheckForm, selectMyPmById } from "../api/manage"
import { getMineForm } from "../api/index"
import { getUserInfoByUserDDId } from "../api/index"
import storage from "../until/index";
import pageStore from '../stores/globalStore'
import create from 'dd-store'


// console.log(storage, 'storage');


// 定义一个变量存储 tabbar的数据
let tabBarData = [
  {
    icon: 'AppstoreOutline',
    activeIcon: 'AppstoreOutline',
    text: '工作台',
  },
  {
    icon: 'PieOutline',
    activeIcon: 'PieOutline',
    text: '指标库',
  },
  {
    icon: 'TeamOutline',
    activeIcon: 'TeamOutline',
    text: '部门绩效',
  },
  {
    icon: 'TeamFill',
    activeIcon: 'TeamFill',
    text: '我的',
  },
]

// 拿到本地缓存的用户级别
// let roleLevelRes = dd.getStorageSync({ key: 'roleLevel' })
// let roleLevel = roleLevelRes.data.value


// 判断用户级别，如果是普通员工，则 tabbar直接赋空
// if (roleLevel == 3) {
//   console.log(roleLevel);
//   tabBarData = []
// }




create.Page({
  store: pageStore,
  data: {
    userDDId: '',
    tabs: tabBarData,
    items4: [
      // {
      //   title: '考评表',
      //   icon: 'CollectMoneyOutline  ',
      // },
      {
        title: '发起考核',
        icon: 'EditSOutline',
      },
      {
        title: '发起评分',
        icon: 'FolderOutline',
      },
      {
        title: '考评设置',
        icon: 'SetOutline',
      },
    ],
    list: [{ label: '全部', value: '001' }],
    launch: [],
    defaultValue: '001',
    current: 0,
    completeWorkTaskCount: 0,
    waitWorkTaskCount: 0,
    checklist: [],
    visual: [],
    // 弹出框的显示状态
    visile: false,
    // 点击标题默认
    defaulTitle: '月度',
    // 考核对应的card数据
    grayBgList: [],
    // select列表
    selectList: [],
    // 绩效列表
    mineCheck: [],
    // 
    checkLisTxt: [],
    pmid: '',
    page: 1,
    size: 10,
    hasMore: true,
    isLoading: false,
    tagid: '',
    // 指定目标时显示的绩效考核标题
    performanceAppraisalTitle: ''
  },
  onLoad() {
    // dd.clearStorageSync()
    dd.setStorage('roleLevel', null)
    dd.setStorage('currentCity', null)
    // 判断当前的用户级别，如果是3，则跳转到我的-页面
    // if (roleLevel == 3) {
    //   this.setData({
    //     current: 3
    //   })
    // }
    dd.getStorage({
      key: 'currentCity',
      success: (res) => {
        console.log(res, 'res');
        let data = {
          userDDId: res.data.value.userId
        }
        getBackLog(data).then((rs) => {
          this.setData({
            completeWorkTaskCount: rs.data.completeWorkTaskCount,
            waitWorkTaskCount: rs.data.waitWorkTaskCount
          })
        })
        getMineForm(data).then((rs) => {
          this.setData({
            mineCheck: rs.data
          })
        })

      },
      fail: function (res) {
        dd.alert({ content: '请稍后再试' });
      }
    });
    let data = {
      page: 1,
      size: 10
    }
    launchCheck(data).then(rs => {
      this.setData({
        checklist: rs.data.list,
        selectList: rs.data.list,
        checkLisTxt: rs.data.list,
        defaulTitle: rs.data.list[0].pmFormName,
        pmid: rs.data.list[0].pmFormId
      })
      this.getgraybg(rs.data.list[0].pmFormId)
      // let newList = rs.data.list.map((item, index) => {
      //   return Object.assign({}, { label: item.pmFormName }, { value: item.pmFormId })
      // })
      // this.setData({
      //   visual: newList,
      //   defaulTitle: newList[0].value
      // })
    })
    getCheckForm().then(rs => {
      rs.data.sort((a, b) => a.sort - b.sort)
      let newList = rs.data.map((item, index) => {
        return Object.assign({}, { label: item.dictName }, { value: item.dictCode })
      })
      let launchList = rs.data.map((item, index) => {
        return Object.assign({}, { label: item.dictName }, { value: item.dictCode })
      })
      newList.unshift({ label: '全部', value: '001' })
      this.setData({
        list: newList,
        launch: launchList
      })
    })
  },
  // 获取考核cardgray数据
  getgraybg(e) {

    let data = {
      pmFormId: e
    }
    allCheck(data).then(res => {
      this.setData({
        grayBgList: res.data
      })
    })
  },
  // 点击跳转考核详情
  checkDetail(e) {
    let pmids = this.data.pmid
    let { tagid, count } = e.currentTarget.dataset
    dd.navigateTo({
      url: '/pages/usual/checkdetail/checkdetail?tagid=' + tagid + '&pmid=' + pmids + '&count=' + count
    })

  },
  onDetail(e) {
    let { checkid, period, status } = e.currentTarget.dataset
    console.log(status);
    if (status == 0) {
      dd.showToast({
        content: "考核已撤销"
      })
      return
    }
    // dd.showLoading({
    //   content: this.$store
    // })
    this.$store.data.performanceAppraisalTitle = e.target.dataset.performanceAppraisalTitle
    this.update()
    dd.navigateTo({
      url: '/pages/usual/checkdetail/checkdetail?period=' + period + '&pmid=' + checkid
    })
  },
  // 点击绩效列表
  handlePopup() {

    this.setData({
      visile: true,
    });
  },
  // 关闭弹出层
  handlePopupClose() {
    this.setData({
      visile: false,
    });
  },
  // 点击列表获得当前ddid请求数据
  handleTap(e) {
    // console.log('点击了跳转');
    let { id, name } = e.currentTarget.dataset
    this.setData({
      pmid: id
    })
    this.getgraybg(id)
    this.setData({
      defaulTitle: name,
      visile: false
    })
  },
  // 模糊匹配
  onChange(value, e) {
    let list = this.data.checklist
    let values = e.detail.value
    let newlists = []
    if (values == "") {
      this.setData({
        selectList: list
      })
    } else {
      if (e.detail.cursor) {
        for (var i = 0; i < list.length; i++) {
          if (list[i].pmFormName.indexOf(values) >= 0) {
            newlists.push(list[i]); //添加搜索到的物品名称
          }
        }
        this.setData({
          selectList: newlists
        })
      }
    }
  },
  // 点击待办任务
  backlog() {
    dd.navigateTo({
      url: '/pages/usual/backlog?tag=1&type=0'
    })
  },
  // 点击已办任务
  finishwork() {
    dd.navigateTo({
      url: '/pages/usual/backlog?tag=2&type=1'
    })
  },
  handleOk(value, column) {
    this.getCheckLisTxt(value, true, 1)
    this.setData({
      tagid: value == '001' ? 'null' : value
    })
  },
  handleChange(index) {
    this.setData({
      current: index
    })
    // // if (roleLevel == 3) {
    // //   console.log('进入');
    // //   this.setData({
    // //     current: 3
    // //   })
    // // }
    // console.log(index, roleLevel);
  },
  getCheckLisTxt(value, refresh = false, page) {
    dd.showLoading({
      content: '加载中...',
    });
    this.setData({
      isLoading: true
    })
    let data = {
      page: page,
      size: this.data.size,
      datePeriodTypeCode: value == '001' ? '' : value
    }
    launchCheck(data).then(rs => {
      const list = rs.data.list;
      const hasMore = list.length >= this.data.size;
      dd.hideLoading();
      this.setData({
        checkLisTxt: refresh ? list : this.data.checkLisTxt.concat(list),
        isLoading: false,
        hasMore,
        page
      })
    })
  },
  onReachBottom() {
    if (this.data.hasMore && !this.data.isLoading) {
      this.getCheckLisTxt('', false, this.data.page + 1)
    }

  },
  handleTapItem(item) {
    console.log(item);
    // dd.showLoading({
    //   content: item.title
    // })
    if (item.title == '考评表') {

    } else if (item.title == '发起考核') {
      dd.navigateTo({
        url: '/pages/manage/launch/launch?launch=' + JSON.stringify(this.data.launch),
      })

      // dd.navigateTo({
      //   url: '/pages/manage/launch/launch'
      // })

    } else if (item.title == '考评设置') {
      dd.navigateTo({
        url: '/pages/manage/setting/setting'
      })
    } else if (item.title == '发起评分') {
      dd.navigateTo({
        url: '/pages/manage/mark/mark'
      })
    }
  },
  // 考评表点击事件

  // 我的绩效-绩效点击事件
  performanceClick(e) {
    let eItem = e.target.dataset.index
    console.log(eItem);
    // 判断是否已撤销
    if (eItem.status == 0) {
      dd.showToast({
        content: "绩效已撤销"
      })
      return
    }

    let gData1 = {
      pmId: eItem.pmId
    }
    let gData2 = {
      userDDId: ''
    }
    let navParamData = {
      pmId: eItem.pmId,
      userName: "",
      avatar: "",
      pmDeptName: ""
    }



    // 根据pmId查询绩效信息
    selectMyPmById(gData1).then((res) => {
      console.log('查询绩效信息', res);
      gData2.userDDId = res.data.pmUserDDId
      navParamData.pmDeptName = res.data.pmDeptName

      // 根据该绩效的归属者userDDId查询用户信息
      getUserInfoByUserDDId(gData2).then((res1) => {
        console.log('该绩效的用户信息', res1);
        navParamData.userName = res1.data.userName
        navParamData.avatar = res1.data.avatar

        // 进行跳转页面
        dd.navigateTo({
          url: `/pages/usual/performancedetail/performancedetail?pmid=${navParamData.pmId}&userName=${navParamData.userName
            }&avatar=${navParamData.avatar}&dept=${navParamData.pmDeptName}&userId=${res1.data.userId}&managerUserId=${res1.data.managerUserDDId}`,
        })
      })
    })
  },
});
